<template>
  <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
    <el-card class="box-card" shadow="never">
      <div class="text item">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="i18n('teacher.picture')" prop="picture">
              <el-upload
                class="avatar-uploader"
                :action="$http.adornUrl('/file/picture')"
                :show-file-list="false"
                :headers="upload_headers"
                :on-success="handlePictureSuccess"
                :before-upload="beforePictureUpload">
                <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
                <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
              </el-upload>
              <el-link @click="handlePictureRemove" type="danger">
                {{ i18n('common.delete') }}
              </el-link>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.name')" prop="name">
                  <el-input
                    v-model="dataForm.name"
                    :placeholder="i18n('common.please_input') + i18n('teacher.name')" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.mobile')" prop="mobile">
                  <el-input
                    v-model="dataForm.mobile"
                    :placeholder="i18n('common.please_input') + i18n('teacher.mobile')" clearable></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.category_id')" prop="category_id">
                  <el-select
                    v-model="dataForm.category_id"
                    :placeholder="i18n('common.please_select') + i18n('teacher.category_id')" clearable>
                    <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.cooperation_level')" prop="cooperation_level">
                  <el-select
                    v-model="dataForm.cooperation_level"
                    :placeholder="i18n('common.please_select') + i18n('teacher.cooperation_level')" clearable>
                    <el-option v-for="(v, k) in cooperationLevelList" :label="v.text" :key="k" :value="v.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.address')" prop="address">
                  <el-cascader v-model="dataForm.address" :placeholder="$t('common.please_select')+$t('teacher.address')" :options="addressList" :props="levelProps" clearable></el-cascader>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.teacher_level')" prop="teacher_level">
                  <el-select
                    v-model="dataForm.teacher_level"
                    :placeholder="i18n('common.please_select') + i18n('teacher.teacher_level')" clearable>
                    <el-option v-for="(v, k) in teacherLevelList" :label="v.text" :key="k" :value="v.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.start_year')" prop="start_year">
                  <el-date-picker
                    v-model="dataForm.start_year"
                    type="year"
                    :placeholder="i18n('common.please_select') + i18n('teacher.start_year')"
                    clearable>
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item :label="i18n('teacher.remark')" prop="remark">
                  <el-input
                    type="textarea"
                    v-model="dataForm.remark"
                    :rows="5"
                    show-word-limit
                    maxlength="200"
                    :placeholder="i18n('common.please_input') + i18n('teacher.remark')">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item :label="i18n('common.sort')" prop="sort">
                  <el-input-number v-model="dataForm.sort" :min="0">
                  </el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item :label="i18n('teacher.is_recommend')" prop="is_recommend">
                  <el-switch
                    v-model="dataForm.is_recommend"
                    active-value="1"
                    :active-text="i18n('common.yes')"
                    inactive-value="2"
                    :inactive-text="i18n('common.no')">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="i18n('common.is_hidden')" prop="is_hidden">
                  <el-switch
                    v-model="dataForm.is_hidden"
                    active-value="1"
                    :active-text="i18n('common.yes')"
                    inactive-value="2"
                    :inactive-text="i18n('common.no')">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :label="i18n('common.status')" prop="status">
                  <el-switch
                    v-model="dataForm.status"
                    active-value="1"
                    :active-text="i18n('common.enable')"
                    inactive-value="2"
                    :inactive-text="i18n('common.disable')">
                  </el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <el-card class="box-card mt10" shadow="never">
      <div class="text item">
        <el-form-item>
          <el-button
            type="primary"
            @click="dataFormSubmit(formRef)">
            {{ i18n('common.confirm') }}
          </el-button>
          <el-button @click="resetForm(formRef)">
            {{ i18n('common.reset') }}
          </el-button>
        </el-form-item>
      </div>
    </el-card>
  </el-form>
</template>

<script setup name="teacher-basic-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const props = defineProps({
    model: {
      type: [String, Object],
      default: null,
    }
  });

  const levelProps = ref({
    label: 'title',
    value: 'id',
    children: 'child',
    checkStrictly: true
  })

  const areaRef = ref()
  const categoryList = ref([])
  const cooperationLevelList = ref([])
  const teacherLevelList = ref([])
  const addressList = ref([])

  const dataForm = reactive({
    id: 0,
    organization_id: '',
    category_id: '',
    name: '',
    mobile: '',
    picture: '',
    start_year: '',
    address: '',
    remark: '',
    cooperation_level: '',
    teacher_level: '',
    sort: 0,
    is_recommend: '2',
    is_hidden: '2',
    status: '1',
  })

  const dataRule = reactive({
    category_id: [{
      required: true,
      message: i18n('teacher.rules.category_id.require'),
      trigger: 'blur'
    }],
    name: [{
      required: true,
      message: i18n('teacher.rules.name.require'),
      trigger: 'blur'
    }],
    picture: [{
      required: true,
      message: i18n('teacher.rules.picture.require'),
      trigger: 'blur'
    }],
    mobile: [{
      required: true,
      message: i18n('teacher.rules.mobile.require'),
      trigger: 'blur'
    }],
    start_year: [{
      required: true,
      message: i18n('teacher.rules.start_year.require'),
      trigger: 'blur'
    }],
    cooperation_level: [{
      required: true,
      message: i18n('teacher.rules.cooperation_level.require'),
      trigger: 'blur'
    }],
    teacher_level: [{
      required: true,
      message: i18n('teacher.rules.teacher_level.require'),
      trigger: 'blur'
    }],
    address: [{
      required: true,
      message: i18n('teacher.rules.address.require'),
      trigger: 'blur'
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(props.model, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${props.model}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.organization_id = data.data.organization_id
            dataForm.category_id = data.data.category_id
            dataForm.name = data.data.name
            dataForm.mobile = data.data.mobile
            dataForm.picture = data.data.picture
            dataForm.start_year = data.data.start_year
            dataForm.address = data.data.address
            dataForm.remark = data.data.remark
            dataForm.cooperation_level = data.data.cooperation_level.value
            dataForm.teacher_level = data.data.teacher_level.value
            dataForm.sort = data.data.sort
            dataForm.is_recommend = data.data.is_recommend.value + ''
            dataForm.is_hidden = data.data.is_hidden.value + ''
            dataForm.status = data.data.status.value + ''
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${props.model}/frist_submit`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            organization_id: dataForm.organization_id,
            category_id: dataForm.category_id,
            name: dataForm.name,
            mobile: dataForm.mobile,
            picture: dataForm.picture,
            start_year: dataForm.start_year,
            address: dataForm.address,
            remark: dataForm.remark,
            cooperation_level: dataForm.cooperation_level,
            teacher_level: dataForm.teacher_level,
            sort: dataForm.sort,
            is_recommend: dataForm.is_recommend,
            is_hidden: dataForm.is_hidden,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/business/category/select'),
      method: 'get',
      params: $http.adornParams({
        type: 1,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadCooperationLevelList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'cooperation_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        cooperationLevelList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadTeacherLevelList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'teacher_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        teacherLevelList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadAddressList = () => {
    $http({
      url: $http.adornUrl('/common/area/second'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        addressList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const handlePictureRemove = () => {
    dataForm.picture = ''
  }

  onMounted(() => {
    init(formRef)

    loadCategoryList()
    loadCooperationLevelList()
    loadTeacherLevelList()
    loadAddressList()
  })
</script>
